#pragma once

#include "Protocol.hpp"

enum{
    Div_Zero = 1,
    Mod_Zero,
    Other_Op
};

class Calculator
{
public:
    Calculator()
    {}

    Response CaculatorHelper(const Request& req)
    {
        Response rsp(0,0);
        switch(req.op)
        {
            case '+' : rsp.result = req.x + req.y; 
            break;
            case '-' : rsp.result = req.x - req.y; 
            break;
            case '*' : rsp.result = req.x * req.y; 
            break;
            case '/' : 
                if(req.y == 0) rsp.code = Div_Zero;
                else rsp.result = req.x / req.y;
            break;
            case '%' : 
                if(req.y == 0) rsp.code = Mod_Zero;
                else rsp.result = req.x % req.y;
            break;
            default:
                rsp.code = Other_Op;
            break;
        }
        return rsp;
    }

    std::string Handler(std::string& package)
    {
        std::string content;
        bool r = Decode(package, &content);
        // lg(Info, "debug: %s", content.c_str());
        if(!r) return "";
        Request req;
        r = req.Deserialize(content);
        if(!r) return "";

        content = "";
        Response rep = CaculatorHelper(req);
        rep.Serialize(&content);
        // lg(Info, "debug: %s", content.c_str());
        std::string res = Encode(content);
        
        return res;
    }

    ~Calculator()
    {}
};